package com.elcl.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @author LCL 2014-11-11
 */
public class StringUtils {

    /**
     * 2014-11-24 LCL
     *
     * @param content
     * @return 判读是否为空或者空字符串
     */
    public static boolean isEmpty(String content) {
        return null == content || content.equals("");
    }

    /**
     * 2014-11-24 LCL
     *
     * @param content
     * @return 与isEmpty的区别是，判断包括空格
     */
    public static boolean isBlank(String content) {
        return null == content || content.equals("") || content.equals("null")
                || content.trim().length() == 0;
    }

    /**
     * 2016-03-03 LCL
     *
     * @return 与isEmpty的区别是，判断包括空格
     */
    public static boolean isNotBlank(String content) {
        return !isBlank(content);
    }

    /**
     * 2016-03-03 LCL
     *
     * @return 与isEmpty的区别是，判断包括空格
     */
    public static boolean isNotBlank(String... content) {
        for (int i = 0, len = content.length; i < len; i++) {
            if (isBlank(content[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * 2014-11-24 LCL
     *
     * @return 与isEmpty的区别是，判断包括空格
     */
    public static boolean isBlank(String... content) {
        for (int i = 0, len = content.length; i < len; i++) {
            if (isBlank(content[i])) {
                return true;
            }
        }
        return false;
    }

    /**
     * 2015-09-09 LCL 将字符串转换为UTF－8格式
     *
     * @return 根据字符串的编码格式转换为UTF－8
     */
    public static String getEncodingResultByCode(String str) {
        String encode = getEncoding(str);
        String result = getEncodingResultByCode(str, encode);
        return result;
    }

    /**
     * 2015-09-09 LCL
     *
     * @return 得到string的编码格式
     */
    public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s = encode;
                return s;
            }
        } catch (Exception exception) {
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s1 = encode;
                return s1;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s2 = encode;
                return s2;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s3 = encode;
                return s3;
            }
        } catch (Exception exception3) {
        }
        return "";
    }

    /**
     * 2015-09-09 LCL
     *
     * @param
     * @return 根据字符串的编码格式转换为UTF－8
     */
    public static String getEncodingResultByCode(String msg, String encode) {
        try {
            byte[] jiema = msg.getBytes(encode);
            String bianma = new String(jiema, "UTF-8");
            return bianma;
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        return "";
    }

    /**
     * 2015-09-09 LCL
     *
     * 网络请求中汉字要进行UTF-8
     * @return 根据字符串的编码格式转换为UTF－8
     */
    public static String urlEncode(String msg) {
        try {
            String result = URLEncoder.encode(msg, "UTF-8");
            return result;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    final static String PLEASE_SELECT = "请选择...";
    public static boolean isEmpty(Object o) {
        return o == null || "".equals(o.toString().trim())
                || "null".equalsIgnoreCase(o.toString().trim())
                || "undefined".equalsIgnoreCase(o.toString().trim())
                || PLEASE_SELECT.equals(o.toString().trim())
                || "\"\"".equals(o.toString().trim());
    }
}
